<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Com\Wechat;
use Com\WechatAuth;
use Think\Log;
use Gaoming13\WechatPhpSdk\Api;
use Admin\Common\MyBll;
header("content-type:text/html;charset=utf-8");
class WeController extends AuthController {
	public function _initialize(){
		$sys_list=M('sys')->where(array('sys_id'=>1))->find();
		$appid=$sys_list['wesys_appid'];
		$token=$sys_list['wesys_token'];
		$crypt=$sys_list['wesys_appsecret'];
			
		$this->token = $token;
		$this->appId = $appid;
		$this->appSecret =$crypt;
		
	}
	
	
	
/************************************自定义菜单模块****************************************/
/*
 * 自定义菜单列表
 */
    public function we_menu(){
   	
    	$nav = new \Org\Util\Leftnav;
    	$we_menu=M('we_menu')->order('we_menu_order')->select();
    	$arr = $nav::menu($we_menu);
    	$menu_top=M('we_menu')->where(array('we_menu_leftid'=>0))->order('we_menu_order')->select();
    	$this->assign('menu_top',$menu_top);
	   	$this->assign('we_menu',$arr);
    	$this->display();
    }

/*
 * 添加自定义菜单方法
 */
    public function we_menu_runadd(){
    	if(!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
                $we_menu=M('we_menu');
                if (I('we_menu_leftid')==0) 
                {
                    $onemenu['we_menu_leftid']=0;
                    $check_menu=$we_menu->where($onemenu)->count();
                    Log::write("一级菜单数量：【".json_encode($check_menu)."】");
                    if ($check_menu>=3){
                        $this->error('菜单数量超出3个，不允许添加',U('we_menu'),0);
                    }
                    else
                    {
                        $sldata=array(
                        'we_menu_leftid'=>I('we_menu_leftid'),
                        'we_menu_name'=>I('we_menu_name'),
                        'we_menu_type'=>I('we_menu_type'),
                        'we_menu_typeval'=>I('we_menu_typeval'),
                        'we_menu_order'=>I('we_menu_order'),
                        'we_menu_open'=>I('we_menu_open'),
                        );
                        $we_menu->add($sldata);
                        $this->success('自定义菜单添加成功',U('we_menu'),1);
                    }
                    
                }
                else
                {
                    $check_menu_two=$we_menu->where(array('we_menu_leftid'=>I('we_menu_leftid')))->count();
                    if ($check_menu_two>=5)
                    {
                        $this->error('子菜单菜单数量超出5个，不允许添加',U('we_menu'),0);
                    }
                    else
                    {
                        $sldata=array(
                        'we_menu_leftid'=>I('we_menu_leftid'),
                        'we_menu_name'=>I('we_menu_name'),
                        'we_menu_type'=>I('we_menu_type'),
                        'we_menu_typeval'=>I('we_menu_typeval'),
                        'we_menu_order'=>I('we_menu_order'),
                        'we_menu_open'=>I('we_menu_open'),
                        );
                        $we_menu->add($sldata);
                        $this->success('自定义菜单添加成功',U('we_menu'),1);
                    }
                }
    	}
    }

/*
 * 自定义菜单状态修改
 */
    public function we_menu_state(){
    	$id=I('x');
    	$statusone=M('we_menu')->where(array('we_menu_id'=>$id))->getField('we_menu_open');//判断当前状态情况
    	if($statusone==1){
    		$statedata = array('we_menu_open'=>0);
    		$auth_group=M('we_menu')->where(array('we_menu_id'=>$id))->setField($statedata);
    		$this->success('状态禁止',1,1);
    	}else{
    		$statedata = array('we_menu_open'=>1);
    		$auth_group=M('we_menu')->where(array('we_menu_id'=>$id))->setField($statedata);
    		$this->success('状态开启',1,1);
    	}
    
    }

/*
 * 自定义菜单排序
 */
    public function we_menu_order(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$we_menu=M('we_menu');
    		foreach ($_POST as $id => $sort){
    			$we_menu->where(array('we_menu_id' => $id ))->setField('we_menu_order' , $sort);
    		}
    		$this->success('排序更新成功',U('we_menu'),1);
    	}
    }

/*
 * 返回修改菜单内容
 */
    public function admin_rule_edit(){
    	$admin_rule=M('auth_rule')->where(array('id'=>I('id')))->find();
    	$this->assign('rule',$admin_rule);
    	$this->display();
    }

/*
 * 修改自定义菜单
 */
    public function admin_rule_runedit(){
        	if(!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$admin_rule=M('auth_rule');
    		$sldata=array(
    				'id'=>I('id'),
    				'name'=>I('name'),
    				'title'=>I('title'),
    				'status'=>I('status'),
    				'css'=>I('css'),
    				'sort'=>I('sort'),
    		);
    		$admin_rule->save($sldata);
    		$this->success('权限修改成功',U('admin_rule'),1);
    	}
    }

/*
 * 删除自定义菜单
 */
    public function we_menu_del(){
    	M('we_menu')->where(array('we_menu_id'=>I('we_menu_id')))->delete();
    	$this->redirect('we_menu');
    }

    

    /*
     * 生成自定义菜单
     */
    
    public function we_menu_make(){
    	$we=M('sys')->where(array('sys_id'=>1))->find();//读取微信配置参数
        $config['appId']=$we["wesys_appid"];
        $config['appSecret']=$we["wesys_appsecret"];

    	
    	/*
    	 * 菜单处理开始
    	 * 只取3条leftid=0的数据
    	*/
    	$we_menu=M('we_menu')->where(array('we_menu_leftid'=>0,'we_menu_open'=>1))->order('we_menu_id')->limit(3)->select();
    	$we_menu_count=M('we_menu')->where(array('we_menu_leftid'=>0,'we_menu_open'=>1))->count();
    	/*
    	 * 菜单数据重组
    	 * 重组结构参考微信公共平台开发文档
    	 * name 菜单名称
    	 * type 菜单类型
    	 * url 链接地址：针对viewleix
    	*/
    	$arr=array();
    	foreach ($we_menu as $v){
    		$we_menu_2=M('we_menu')->where(array('we_menu_leftid'=>$v['we_menu_id'],'we_menu_open'=>1))->find();
    		if (!empty($we_menu_2)){
    			$v['n']=1;
    		}else{
    			$v['n']=0;
    		}
    		$arr[]=$v;
    	}
    	//字段N 判断是否有下一级
    	//p($arr);die;
    	$k=0;
    	$data = '{"button":[';//菜单头
    	foreach($arr as $v){
    		if ($v['we_menu_type']=='click'){
    			$type='key';
    		}elseif ($v['we_menu_type']=='view'){
    			$type='url';
    		}
    		$k=$k+1;
    		if ($v['n']==0){
    			if ($k==$we_menu_count){//如果是最后一个菜单，那么就不要逗号；
    				$data.='{"type":"'.$v['we_menu_type'].'","name":"'.$v['we_menu_name'].'","'.$type.'":"'.$v['we_menu_typeval'].'"}';//如果是最后一个菜单，不就要逗号了。
    			}else{
    				$data.='{"type":"'.$v['we_menu_type'].'","name":"'.$v['we_menu_name'].'","'.$type.'":"'.$v['we_menu_typeval'].'"},';//如果是最后一个菜单，不就要逗号了。
    			}
    		}else{
    			$data.='{"name":"'.$v['we_menu_name'].'","sub_button":[';
    			/*
    			 * 开始输出子菜单
    			 */
    			$we_menu_two=M('we_menu')->where(array('we_menu_leftid'=>$v['we_menu_id'],'we_menu_open'=>1))->select();//子菜单数据
    			$we_menu_twocount=M('we_menu')->where(array('we_menu_leftid'=>$v['we_menu_id'],'we_menu_open'=>1))->count();//子菜单数量
    			$m=0;
    			foreach ($we_menu_two as $y){
    				if ($y['we_menu_type']=='click'){
    					$type_two='key';
    				}elseif ($y['we_menu_type']=='view'){
    					$type_two='url';
    				}
    				$m=$m+1;
    				
    				if ($m==$we_menu_twocount){//如果是最后一个菜单，那么就不要逗号；
    					$data.='{"type":"'.$y['we_menu_type'].'","name":"'.$y['we_menu_name'].'","'.$type_two.'":"'.$y['we_menu_typeval'].'"}';//如果是最后一个菜单，不就要逗号了。
    				}else{
    					$data.='{"type":"'.$y['we_menu_type'].'","name":"'.$y['we_menu_name'].'","'.$type_two.'":"'.$y['we_menu_typeval'].'"},';//如果是最后一个菜单，不就要逗号了。
    				}
    			}
    			if ($m==$we_menu_twocount){//如果是最后一个菜单的话，就不要逗号了
    				$data.= ']},';
    			}else{
    				$data.= ']}';
    			}
    		}
    	}
    	$data.= ']}';
    	ob_clean();
        Log::write("最终的菜单:【".$data."】");
        $weCath = new \Gaoming13\WechatPhpSdk\Api($config);
        $bll=new MyBll();
        $access_token= $bll->Get_accessToken();
        Log::write("获取的access_token:【".json_encode($access_token)."】");
        $relval=$weCath->create_menu($data,$access_token);
        Log::write("生成菜单的结果:【".json_encode($relval)."】");
        if($relval->errmsg=="ok")
        {
            $this->success('菜单设置成功。',U('we_menu'),1);
        }else
        {
           $this->success('菜单设置失败。',U('we_menu'),2);
        }
    }
/*
 * 修改自定义菜单显示
 */
    public function we_menu_edit(){
    	$we_menu_id=I('we_menu_id');
    	$we_menu=M('we_menu')->where(array('we_menu_id'=>$we_menu_id))->find();
    	$sl_data['we_menu_id']=$we_menu['we_menu_id'];
    	$sl_data['we_menu_name']=$we_menu['we_menu_name'];
    	$sl_data['we_menu_leftid']=$we_menu['we_menu_leftid'];
    	$sl_data['we_menu_type']=$we_menu['we_menu_type'];
    	$sl_data['we_menu_typeval']=$we_menu['we_menu_typeval'];
    	$sl_data['we_menu_order']=$we_menu['we_menu_order'];
    	$sl_data['status']=1;
    	$this->ajaxReturn($sl_data,'json');
    }
    
/*
 * 修改自定义菜单方法
 */
    public function we_menu_runedit(){
    	if (!IS_AJAX){
    		$this->error('提交方式不正确',0,0);
    	}else{
    		$sl_data=array(
    				'we_menu_id'=>I('we_menu_id'),
    				'we_menu_name'=>I('we_menu_name'),
    				'we_menu_leftid'=>I('we_menu_leftid'),
    				'we_menu_type'=>I('we_menu_type'),
    				'we_menu_typeval'=>I('we_menu_typeval'),
    				'we_menu_order'=>I('we_menu_order'),
    
    		);
    		M('we_menu')->save($sl_data);
    		$this->success('自定义菜单修改成功',U('we_menu'),1);
    	} 
    }
    
    
/*************************************************************************************/
    
    public function we_reply_text(){
    	$we_reply=M('we_reply');
    	$key=I('key');
    		$map['we_reply_key']= array('like',"%".$key."%");
    		$map['we_reply_type']=array('eq',text);
    	$count= $we_reply->where($map)->count();
    	$Page= new \Think\Page($count,C('DB_PAGENUM_10'));
    	$show= $Page->show(); 
    	
    	$we_reply_list=$we_reply->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('we_reply_addtime desc')->select();
    	$this->assign('we_reply_list',$we_reply_list);
    	$this->assign('page',$show);
    	$this->display();
    }
    
    
    public function we_replytext_runadd(){
    	if (!IS_AJAX){
    		$this->error("提交方式错误！",U('we_reply_text'),0);
    	}else{
    		$we_reply=M('we_reply');
    		if (!$we_reply->autoCheckToken($_POST)){
    			$this->error('重复提交，请刷新后操作',U('we_reply_text'),0);
    		}else{
    			$we_reply_key=I('we_reply_key');
    			$map['we_reply_key']=$we_reply_key;
    			$check_key=$we_reply->where($map)->find();
    				if (!empty($check_key)){
    					$this->error("关键字【".$we_reply_key.'】已存在，请重新输入',U('we_reply_text'),0);
    				}
    			
    			
    			$sl_data=array(
    					'we_reply_key'=>I('we_reply_key'),
    					'we_replytext_content'=>I('we_replytext_content'),
    					'we_reply_addtime'=>time(),
    					'we_reply_type'=>'text',
    			);
    			$we_reply->field('we_reply_key,we_replytext_content,we_reply_addtime,we_reply_type')->add($sl_data);
    			$this->success('微信自动回复关键字保存成功',U('we_reply_text'),1);
    			 
    		}
    	}
    }
    
    public function we_reply_del(){
    	$we_reply_list=M('we_reply');
    	$p=I('p');
    	$we_reply_id=I('we_reply_id');
    	$check_replytext_id=$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->find();
    	if (empty($check_replytext_id)){
    		$this->error('参数不正确',U('we_reply_text'),0);
    	}else {
			$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->delete();
			$this->redirect('we_reply_text', array('p' => $p));
    	}
    }
    
    public function we_replytext_edit(){
    	$we_reply_id=I('we_reply_id');
    	$p=I('p_l');
    	$we_reply_list=M('we_reply');
    	
    	$replytext_list=$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->find();
    	
    	$sl_data['we_reply_id']=$replytext_list['we_reply_id'];
    	$sl_data['we_reply_key']=$replytext_list['we_reply_key'];
    	$sl_data['we_replytext_content']=$replytext_list['we_replytext_content'];
    	$sl_data['p']=$p;
    	$sl_data['status']  = 1;
    	$this->ajaxReturn($sl_data,'json');
    }
    
    public function we_replytext_runedit(){
        	if (!IS_AJAX){
    		$this->error("提交方式错误！",U('we_reply_text'),0);
    	}else{
    		$we_reply=M('we_reply');
    		if (!$we_reply->autoCheckToken($_POST)){
    			$this->error('重复提交，请刷新后操作',U('we_reply_text'),0);
    		}else{
    			$we_reply_key=I('we_reply_key');
    			$map['we_reply_key']=$we_reply_key;
    				$map['we_reply_id']=array('neq',I('we_reply_id'));
    				$check_key=$we_reply->where($map)->find();
    				if (!empty($check_key)){
    					$this->error("关键字【".$we_reply_key.'】已存在，请重新输入',U('we_reply_text'),0);
    				}
    			
    			$sl_data=array(
    					'we_reply_id'=>I('we_reply_id'),
    					'we_reply_key'=>I('we_reply_key'),
    					'we_replytext_content'=>I('we_replytext_content'),
    			);
    			$we_reply->field('we_reply_id,we_reply_key,we_replytext_content')->save($sl_data);
    			$this->success('微信自动回复关键字保存成功',U('we_reply_text',array('p'=>I('p'))),1);
    			 
    		}
    	}
    	
    }
    
/*******************************************图片回复******************************************************/
    public function we_reply_pic(){
    	$we_reply=M('we_reply');
    	$key=I('key');
    		$map['we_reply_key']= array('like',"%".$key."%");
    		$map['we_reply_type']=array('eq',news);
    		
    	$count= $we_reply->where($map)->count();
    	$Page= new \Think\Page($count,C('DB_PAGENUM_10'));
    	$show= $Page->show(); 
    	
    	$we_reply_list=$we_reply->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('we_reply_addtime desc')->select();
    	$this->assign('we_reply_list',$we_reply_list);
    	$this->assign('page',$show);
    	$this->display();
    }
    
    public function we_reply_pic_del(){
    	$we_reply_list=M('we_reply');
    	$p=I('p');
    	$we_reply_id=I('we_reply_id');
    	$check_replytext_id=$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->find();
    	if (empty($check_replytext_id)){
    		$this->error('参数不正确',U('we_reply_pic'),0);
    	}else {
    		$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->delete();
    		$this->redirect('we_reply_pic', array('p' => $p));
    	}
    }
    
    
    public function we_replypic_runadd(){
    	$reply_pic=M('we_reply');
    	$we_reply_key=I('we_reply_key');

        $map['we_reply_key']=$we_reply_key;
        $check_key=$reply_pic->where($map)->find();
            if (!empty($check_key))
            {
                $this->success("关键字【".$we_reply_key.'】已存在，请重新输入',U('we_reply_pic'),0);
            }
            else
            {
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize   =     3145728 ;// 设置附件上传大小
                    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath  =     './Public/uploads/we_reply_pic/'; // 设置附件上传根目录
                    $upload->savePath  =     ''; // 设置附件上传（子）目录
                    $upload->saveRule  =     'time';
                    $info   =   $upload->upload();

                    if ($info){
                        $img_url='/uploads/we_reply_pic/'.$info['we_replynews_image']['savepath'].$info['we_replynews_image']['savename'];
                    }else{
                        $this->error($upload->getError());//否则就是上传错误，显示错误原因
                    }
                    $sl_data=array(
                            'we_reply_key'=>$we_reply_key,
                            'we_reply_type'=>'news',
                            'we_reply_addtime'=>time(),
                            'we_replynews_image'=>$img_url,
                            'we_replynews_title'=>I('we_replynews_title'),
                            'we_replynews_abs'=>I('we_replynews_abs'),
                            'we_replynews_url'=>I('we_replynews_url'),
                    );
                    
                    $reply_pic->add($sl_data);
                    $this->success('保存成功',U('we_reply_pic'),1);
            }
    	
    }
    
    public function we_replypic_edit()
    {
        $we_reply_id=I('we_reply_id');
        $p=I('p_l');
        $we_reply_list=M('we_reply');
        
        $replypic_list=$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->find();
        
        $sl_data['we_reply_id']=$replypic_list['we_reply_id'];
        $sl_data['we_reply_key']=$replypic_list['we_reply_key'];
        $sl_data['we_replynews_image']=$replypic_list['we_replynews_image'];
        $sl_data['we_replynews_title']=$replypic_list['we_replynews_title'];
        $sl_data['we_replynews_abs']=$replypic_list['we_replynews_abs'];
        $sl_data['we_replynews_url']=$replypic_list['we_replynews_url'];

        $sl_data['p']=$p;
        $sl_data['status']  = 1;
        $this->ajaxReturn($sl_data,'json');
    }
    
    public function we_replypic_runedit(){

    	$we_reply_key=I('we_reply_key');
    	$we_reply_id=I('we_reply_id');
        $map['we_reply_key']=$we_reply_key;
            $map['we_reply_id']=array('neq',I('we_reply_id'));
            $check_key=M("we_reply")->where($map)->find();
            if (!empty($check_key))
            {
                $this->success("关键字【".$we_reply_key.'】已存在，请重新输入',U('we_reply_pic'),0);
            }
            else
            {
                Log::write("图片信息:【".json_encode($_FILES)."】");
                if ($_FILES['we_replynews_image']['name']) 
                {
                    Log::write("图片信息:【".json_encode($_FILES['we_replynews_image']['name'])."】");
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize   =     3145728 ;// 设置附件上传大小
                    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath  =     './Public/uploads/we_reply_pic/'; // 设置附件上传根目录
                    $upload->savePath  =     ''; // 设置附件上传（子）目录
                    $upload->saveRule  =     'time';
                    $info   =   $upload->upload();
                
                    if ($info){
                        $img_url='/uploads/we_reply_pic/'.$info['we_replynews_image']['savepath'].$info['we_replynews_image']['savename'];
                    }
                    $sl_data['we_replynews_image']=$img_url;
                    $sl_data['we_reply_key']=I('we_reply_key');
                    $sl_data['we_replynews_title']=I('we_replynews_title');
                    $sl_data['we_replynews_abs']=I('we_replynews_abs');
                    $sl_data['we_replynews_url']=I('we_replynews_url');
                    $res=M("we_reply")->where("we_reply_id={$we_reply_id}")->save($sl_data);
                    $this->success('保存成功',U('we_reply_pic'),1);
                }
                else
                {
                    $old_img=M("we_reply")->where("we_reply_id={$we_reply_id}")->find();
                    $sl_data['we_replynews_image']=$old_img['we_replynews_image'];
                    $sl_data['we_reply_key']=I('we_reply_key');
                    $sl_data['we_replynews_title']=I('we_replynews_title');
                    $sl_data['we_replynews_abs']=I('we_replynews_abs');
                    $sl_data['we_replynews_url']=I('we_replynews_url');
                    $res=M("we_reply")->where("we_reply_id={$we_reply_id}")->save($sl_data);
                    $this->success('保存成功',U('we_reply_pic'),1);
                }
            }

    }
    
    /*******************************关注回复***************************/
    
    public function we_reply_hello()
    {
        //查询
        $hhhh['we_reply_type']='hello';
        $info=M("we_reply")->where($hhhh)->find();
        //print_r($info);
        $this->assign('info',$info);
        $this->display();
    }
    public function we_reply_runaddhello()
    {
        Log::write("提交的数据:【".json_encode($_POST)."】");
        if (I("hello_type")=='text') 
        {
            //搜索数据库是否已经设置关注回复
            $hhhh['we_reply_type']='hello';
            $repeat=M("we_reply")->where($hhhh)->find();
            if ($repeat) 
            {
                $this->success('你已经设置过关注回复了。',U('we_reply_hello'),1);
            }
            else
            {
                $data['we_replytext_content']=I('we_replytext_content');
                $data['we_reply_type']='hello';
                $data['we_hello_type']=1;
                $data['we_reply_addtime']=time();
                $res=M("we_reply")->add($data);
                if ($res) 
                {
                     $this->success('保存成功',U('we_reply_hello'),1);
                }
            }
        }
        else
        {
            //搜索数据库是否已经设置关注回复
            $hhhh['we_reply_type']='hello';
            $repeat=M("we_reply")->where($hhhh)->find();
            Log::write("是否设置过了：【".json_encode($repeat)."】");
            if ($repeat) 
            {
                $this->success('你已经设置过关注回复了。',U('we_reply_hello'),1);
            }
            else
            {

                if ($_FILES['we_replynews_image']['name']) 
                {
                    Log::write("图片信息:【".json_encode($_FILES['we_replynews_image']['name'])."】");
                    $upload = new \Think\Upload();// 实例化上传类
                    $upload->maxSize   =     3145728 ;// 设置附件上传大小
                    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->rootPath  =     './Public/uploads/we_reply_pic/'; // 设置附件上传根目录
                    $upload->savePath  =     ''; // 设置附件上传（子）目录
                    $upload->saveRule  =     'time';
                    $info   =   $upload->upload();
                    if ($info){
                        $img_url='/uploads/we_reply_pic/'.$info['we_replynews_image']['savepath'].$info['we_replynews_image']['savename'];
                    }
                    $sl_data['we_replynews_image']=$img_url;
                    $sl_data['we_replynews_title']=I('we_replynews_title');
                    $sl_data['we_replynews_abs']=I('we_replynews_abs');
                    $sl_data['we_replynews_url']=I('we_replynews_url');
                    $sl_data['we_reply_type']='hello';
                    $sl_data['we_hello_type']=2;
                    $sl_data['we_reply_addtime']=time();
                    $res=M("we_reply")->add($sl_data);
                    if ($res) 
                    {
                         $this->success('保存成功',U('we_reply_hello'),1);
                    }
                }
                else
                {
                    $this->success('未上传图片',U('we_reply_hello'),1);
                }
                
            }
        }

    }
    
    public function del_hello()
    {
        $we_reply_list=M('we_reply');
        $p=I('p');
        $we_reply_id=I('we_reply_id');
        $check_replytext_id=$we_reply_list->where(array('we_reply_id'=>$we_reply_id))->find();
        if (empty($check_replytext_id)){
            $this->error('参数不正确',U('we_reply_text'),0);
        }else {
            $we_reply_list->where(array('we_reply_id'=>$we_reply_id))->delete();
            $this->redirect('we_reply_hello', array('p' => $p));
        }
    }

    /**
     * 资源文件上传方法
     * @param  string $type 上传的资源类型
     * @return string       媒体资源ID
     */
    private function upload($type,$val){
    	if($token){
    		$we_auth = new WechatAuth($this->appId, $this->appSecret,$this->token);
    	} else {
    		$we_auth  = new WechatAuth($this->appId, $this->appSecret);
    		$token = $we_auth->getAccessToken();
    
    		session(array('expire' => $token['expires_in']));
    		session("token", $token['access_token']);
    	}
    
    	switch ($type) {
    		case 'image':
    			$filename = $val;
    			$media    = $we_auth->materialAddMaterial($filename, $type);
    			break;
    
    		case 'voice':
    			$filename = './Public/voice.mp3';
    			$media    = $we_auth->materialAddMaterial($filename, $type);
    			break;
    
    		case 'video':
    			$filename    = './Public/video.mp4';
    			$discription = array('title' => '视频标题', 'introduction' => '视频描述');
    			$media       = $we_auth->materialAddMaterial($filename, $type, $discription);
    			break;
    
    		case 'thumb':
    			$filename = './Public/music.jpg';
    			$media    = $we_auth->materialAddMaterial($filename, $type);
    			break;
    
    		default:
    			return '';
    	}
    
    	if($media["errcode"] == 42001){ //access_token expired
    		session("token", null);
    		$this->upload($type);
    	}
    
    	return $media['media_id'];
    }
    
    private function del_material($media_id){
    	if($token){
    		$we_auth = new WechatAuth($this->appId, $this->appSecret,$this->token);
    	} else {
    		$we_auth  = new WechatAuth($this->appId, $this->appSecret);
    		$token = $we_auth->getAccessToken();
    	
    		session(array('expire' => $token['expires_in']));
    		session("token", $token['access_token']);
    	}
    	$we_auth->del_material($media_id);

    }
    public function user_list()
    {
        $key=I('key');
        $map['nickname'] = array('like',"%".$key."%");
        /*
         * 分页操作
         */
        $count= M('we_userlist')->where($map)->count();// 查询满足要求的总记录数
        $Page= new \Think\Page($count,30);// 实例化分页类 传入总记录数和每页显示的记录数
        $show= $Page->show();// 分页显示输出
        
        $user_list=M('we_userlist')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('time desc')->select();
        $this->assign('user_list',$user_list);
        $this->assign('page',$show);
        $this->assign('val',$key);
        $this->display();
    }
    public function get_user_list()
    {
        $we=M('sys')->where(array('sys_id'=>1))->find();//读取微信配置参数
        $config['appId']=$we["wesys_appid"];
        $config['appSecret']=$we["wesys_appsecret"];
        $bll=new MyBll();
        $actoken= $bll->Get_accessToken();
        Log::write("【获取的actoken:".json_encode($actoken)."】");
        $weCath = new \Gaoming13\WechatPhpSdk\Api($config);
        
        Log::write("【获取的config:".json_encode($config)."】");
        $relval=$weCath->get_user_list($actoken);
        Log::write("【获取的relval:".json_encode($relval)."】");
        if(isset($relval->total))
        {
            Log::write("【进入判断】");
            $listArr=$relval->data->openid;
            Log::write("【获取的listArr：".json_encode($listArr)."】");
            foreach($listArr as $va)
            {
                //调用接口获取该用户的信息
                $actoken= $bll->Get_accessToken();
                $userinfo=$weCath->get_user_info($va,"zh_CN",$actoken);
                Log::write("获取的userInfo:【".json_encode($userinfo)."】");
                $data=array(
                    "openid"=>$userinfo->openid,
                    "nickname"=>$userinfo->nickname,
                    "head"=>$userinfo->headimgurl,
                    "sex"=>$userinfo->sex,
                    "city"=>$userinfo->city,
                    "language"=>$userinfo->language,
                    "time"=>date('Y-m-d H:i:s', $userinfo->subscribe_time)
                );
                $old= M("we_userlist")->where("openid='".$va."'")->select();
                Log::write("OLD:【".json_encode($old)."】");
                if(count($old)>0)
                {
                    //M("userinfo")->where("openid='".$va."'")->save($data);
                }else
                {
                    M("we_userlist")->add($data);
                    
                }
            }
        }
        $this->redirect('We/user_list');
    }
    
    
}